package com.boost.bio.line;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

public class SingleReadLine implements ReadLine {
    private CharSplitBufferedReader charSplitBufferedReader;
    private char splitLine;
    private char splitFiled;
    private MyInputStream myInputStream;
    public SingleReadLine(String filePath,char splitLine,char splitFiled) throws IOException {
        myInputStream = new MyInputStream(filePath);
        this.splitFiled=splitFiled;
        this.splitLine=splitLine;
        charSplitBufferedReader = new CharSplitBufferedReader(new InputStreamReader(myInputStream.getInputStream(), Charset.forName("UTF-8")),splitLine);
    }

    @Override
    public List<String> read() throws IOException {
        String s = charSplitBufferedReader.readLine();
        if(StringUtils.isBlank(s)){
            return null;
        }
        return  Arrays.asList(s.split(String.valueOf(splitFiled)));
    }

    @Override
    public void close() throws IOException {
        charSplitBufferedReader.close();
        myInputStream.close();
    }
}
